package org.ting.j20250110_gobang.game;

import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class RoomManager {
    private ConcurrentHashMap<String, Room> roomIdToRoom = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, String> userIdToRoomId = new ConcurrentHashMap<>();
    public void add(String roomId, Room room, Integer userId1, Integer userId2) {
        roomIdToRoom.put(roomId, room);
        userIdToRoomId.put(userId1, roomId);
        userIdToRoomId.put(userId2, roomId);
    }
    public void remove(String roomId, int userId1, int userId2) {
        roomIdToRoom.remove(roomId);
        userIdToRoomId.remove(userId1);
        userIdToRoomId.remove(userId2);
    }
    public Room getRoomByRoomId(String roomId) {
        return roomIdToRoom.get(roomId);
    }
    public Room getRoomByUserId(Integer userId) {
        return roomIdToRoom.get(userIdToRoomId.get(userId));
    }
}
